//
//  AutoDismissView.m
//  EasyB
//
//  Created by Timmy on 12/31/11.
//  Copyright 2011 Morgen Works. All rights reserved.
//

#import "TinyView.h"
#import <QuartzCore/QuartzCore.h>

@interface TinyView ()
- (void)dismiss;
@end

@implementation TinyView

@synthesize timeInterval = _timeInterval;

- (id)initTarget:(UIView *)target
{
    CGRect frame = CGRectMake(0, 0, DefaultTinyViewWidth, DefaultTinyViewHeight);
    self = [super initWithFrame:frame];
    
    if (self) {
        self.center = target.center;
        _timeInterval = 2.0f;
        [target addSubview:self];
        [self setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin ];
        self.backgroundColor = [UIColor darkGrayColor];
        self.alpha = 0.8;
        self.layer.masksToBounds = NO;
        self.layer.cornerRadius = 5; // if you like rounded corners
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];

}

- (void)autoDismiss
{
    [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(dismiss) userInfo:nil repeats:NO];
}

- (void)dismiss
{
    [self removeFromSuperview];
}

+ (TinyView *)indicatorWithTarget:(UIView *)target
{
    TinyView *tiny = [[TinyView alloc] initTarget:target];
    tiny.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    
    UIActivityIndicatorView *tmpIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    CGRect iframe = tmpIndicator.frame;
    iframe.origin.x = tiny.frame.size.width/2-iframe.size.width/2;
    iframe.origin.y = tiny.frame.size.height/2-iframe.size.height/2;
    tmpIndicator.frame = iframe;
    [tmpIndicator startAnimating];
    
    [tiny addSubview:tmpIndicator];
    [tmpIndicator release];

    return [tiny autorelease];
}


+ (void)autoDismissMessageWithTarget:(UIView *)target timeinterval:(float)interval message:(NSString *)msg
{
    CGSize maximumLabelSize = CGSizeMake(100000, 60);
    UIFont *font = [UIFont boldSystemFontOfSize:20];
    CGSize expectedLabelSize = [msg sizeWithFont:font 
                                      constrainedToSize:maximumLabelSize 
                                          lineBreakMode:UILineBreakModeTailTruncation]; 
    
    TinyView *tiny = [[TinyView alloc] initTarget:target];
    tiny.timeInterval = interval;
    CGRect frame = tiny.frame;
    frame.size.width = expectedLabelSize.width;
    frame.size.height = maximumLabelSize.height;
    frame.origin.x = (target.frame.size.width - frame.size.width) / 2;
    frame.origin.y = (target.frame.size.height - frame.size.height) / 2;
    tiny.frame = frame;
    
    UILabel *label = [[UILabel alloc] initWithFrame:tiny.bounds];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = UITextAlignmentCenter;
    label.text = msg;
    [tiny addSubview:label];
    [label release];
    
    [tiny autoDismiss];
    [tiny release];

}


@end
